package com.riant.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import javax.sql.DataSource;

/**
 * @Classname TxAdvice
 * @Description TODO
 * @Date 2021/8/17 23:50
 * @Created by 张斌
 */
@Aspect
public class TxAdvice {
    @Autowired
    private DataSource dataSource;
    @Pointcut("execution(* *..transfer(..))")
    public void transtruction(){
    }
    @Around("transtruction()")
    public Object transtructionManager(ProceedingJoinPoint pjp) throws Throwable {
        PlatformTransactionManager ptm = new DataSourceTransactionManager(this.dataSource);
        TransactionDefinition td = new DefaultTransactionDefinition();
        TransactionStatus ts = ptm.getTransaction(td);
        Object ret = pjp.proceed(pjp.getArgs());
        ptm.commit(ts);
        return ret;
    }

}

